Partial Public Class Grupo

    Private objDesglose As Desglose

    Public Sub insertarEstudiante(ByVal pnombre As String, ByVal papellido As String, ByVal pcarne As String, ByVal pemail As String, ByVal ppass As String)
        Dim objEst As New Estudiante
        objEst.insertarDatos(pnombre, papellido, pcarne, pemail, Me.idGrupo, ppass)
        objEst.insertese()
    End Sub


    Public Sub modificarEstudiante(ByVal pnombre As String, ByVal papellido As String, ByVal pcarne As String, ByVal pemail As String, ByVal pidE As Integer, ByVal ppass As String)
        Dim objEst As New Estudiante(pidE)
        objEst.insertarDatos(pnombre, papellido, pcarne, pemail, Me.idGrupo, ppass)
        objEst.actualicese()
    End Sub



    Public Sub eliminarEstudiante(ByVal pidE As Integer)
        Try
            Dim da As New accesoBD.TripleCDataSetTableAdapters.QueriesTableAdapter
            da.deleteEstudiante(Me.idGrupo, pidE)
        Catch ex As Exception
            Throw New ArgumentException("El estudiante tiene evaluaciones")
        End Try
    End Sub



    Public Function obtenerEstudiantes() As DataTable
        Dim dt As DataTable = New accesoBD.TripleCDataSetTableAdapters.selectEstudiantesTableAdapter().GetData(Me.idGrupo)
        Return dt
    End Function

    Public Function obtenerLista(ByVal pidR As Integer) As DataTable
        Dim idE As Integer = 0
        Dim dt As New DataTable
        Me.obtenerPlan()
        Dim objR As Rubro = objP.obtenerRubro(pidR)
        If (objR.getTipo().Equals("individual")) Then
            dt = New accesoBD.TripleCDataSetTableAdapters.selectEstudiantes_NotaTableAdapter().GetData(Me.idGrupo)
            dt = Me.hagaLista(dt, pidR)
        ElseIf (objR.getTipo().Equals("grupal")) Then
            dt = New accesoBD.TripleCDataSetTableAdapters.selectEquipos_NotaTableAdapter().GetData(Me.idGrupo)
            dt = Me.hagaListaEquipo(dt, pidR)
        End If
        Return dt
    End Function

    Private Function hagaLista(ByVal datos As DataTable, ByVal pidR As Integer) As DataTable
        Dim i, idE, j, num As Integer
        Dim numeros As New ArrayList
        Dim dt As New DataTable
        For i = 0 To datos.Rows.Count - 1
            idE = datos.Rows.Item(i).Item(0)
            dt = New accesoBD.TripleCDataSetTableAdapters.obtenerEstudiante_EvaluacionTableAdapter().GetData(idE, pidR)
            If (dt.Rows.Count > 0) Then
                numeros.Add(i)
            End If
        Next
        j = numeros.Count - 1
        While (j >= 0)
            num = CInt(numeros.Item(j))
            datos.Rows.RemoveAt(num)
            j = j - 1
        End While
        Return datos
    End Function

    Private Function hagaListaEquipo(ByVal datos As DataTable, ByVal pidR As Integer) As DataTable
        Dim i, idE, j, num As Integer
        Dim numeros As New ArrayList
        Dim dt As New DataTable
        For i = 0 To datos.Rows.Count - 1
            idE = datos.Rows.Item(i).Item(0)
            dt = New accesoBD.TripleCDataSetTableAdapters.obtenerEquipo_EvaluacionTableAdapter().GetData(idE, pidR)
            If (dt.Rows.Count > 0) Then
                numeros.Add(i)
            End If
        Next
        j = numeros.Count - 1
        While (j >= 0)
            num = CInt(numeros.Item(j))
            datos.Rows.RemoveAt(num)
            j = j - 1
        End While
        Return datos
    End Function


    '--------------------------------------------------------------------------------------
    '--------------------------------------------------------------------------------------
    Public Function obtenerListaEvaluada(ByVal pidR As Integer) As DataTable
        Me.obtenerPlan()
        Dim objR As Rubro = objP.obtenerRubro(pidR)
        Me.objDesglose = New Desglose()
        Return Me.objDesglose.obtenerListaEvaluada(objR, Me.idGrupo, pidR)
    End Function
    '--------------------------------------------------------------------------------------
    '--------------------------------------------------------------------------------------

    Public Sub asignarNota(ByVal pidE As Integer, ByVal pidR As Integer, ByVal pnota As String)
        Me.obtenerPlan()
        Dim objR As Rubro = objP.obtenerRubro(pidR)
        If (objR.getTipo().Equals("individual")) Then
            Dim objE As New Estudiante(pidE, Me.idGrupo)
            objE.asignarNota(pidR, pnota)
        ElseIf (objR.getTipo().Equals("grupal")) Then
            Dim objT As New Equipo(pidE, Me.idGrupo)
            objT.asignarNota(pidR, pnota)
        End If
    End Sub

    Public Sub darNota(ByVal pidE As Integer, ByVal pidR As Integer, ByVal pnota As String)
        Me.obtenerPlan()
        Dim objR As Rubro = objP.obtenerRubro(pidR)
        If (objR.getTipo().Equals("individual")) Then
            Dim objE As New Estudiante(pidE)
            objE.darNota(pidR, pnota)
        ElseIf (objR.getTipo().Equals("grupal")) Then
            Dim objT As New Equipo(pidE)
            objT.darNota(pidR, pnota)
        End If
    End Sub


    Public Sub modificarNota(ByVal pidE As Integer, ByVal pidR As Integer, ByVal pnota As String)
        Me.obtenerPlan()
        'Throw New ArgumentException("Pasa")
        Dim objR As Rubro = objP.obtenerRubro(pidR)
        'Throw New ArgumentException("Pasa" & objR.getTipo)
        If (objR.getTipo().Equals("individual")) Then
            Dim objE As New Estudiante(pidE)
            objE.asignarNota(pidR, pnota)
        ElseIf (objR.getTipo().Equals("grupal")) Then
            Dim objE As New Estudiante(pidE)
            objE.modificarNota(Me.idGrupo, pidR, pnota)
        End If
    End Sub


    Public Function mostrarDesgloseEstudiante(ByVal pidE As Integer) As DataTable
        Dim objE As New Estudiante(pidE)
        Return objE.obtenerDesglose()
    End Function

    Public Function mostrarPromedioEstudiante(ByVal pidE As Integer) As ArrayList
        Dim objE As New Estudiante(pidE)
        Return objE.obtenerPromedio()
    End Function

    Public Sub eliminarNota(ByVal pidR As Integer, ByVal pidN As Integer)
        Me.objDesglose = New Desglose()
        Me.obtenerPlan()
        Dim objR As Rubro = objP.obtenerRubro(pidR)
        Me.objDesglose.eliminarNota(pidR, pidN, Me.idGrupo, objR)
    End Sub

    Public Function obtenerEquipos() As DataTable
        Dim dt As New DataTable
        dt = New accesoBD.TripleCDataSetTableAdapters.selectEquipos_NotaTableAdapter().GetData(Me.idGrupo)
        Return dt
    End Function

    Public Function traigaEstudiantesLibre() As DataTable
        Dim dt As New DataTable
        dt = New accesoBD.TripleCDataSetTableAdapters.selectEstudiantes_SinEquipoTableAdapter().GetData(Me.idGrupo)
        Return dt
    End Function

    Public Function traigaEstudiantesEquipo(ByVal pidE As Integer) As DataTable
        Dim dt As New DataTable
        dt = New accesoBD.TripleCDataSetTableAdapters.selectEstudiantesDelEquipoTableAdapter().GetData(Me.idGrupo, pidE)
        Return dt
    End Function

    Public Sub incluirEstudianteEquipo(ByVal pidEq As Integer, ByVal pidE As Integer)
        Dim dt As New DataTable
        dt = New accesoBD.TripleCDataSetTableAdapters.selectNotasEquipo_EquipoTableAdapter().GetData(pidEq)
        If Not (dt.Rows.Count > 0) Then
            Dim da As New accesoBD.TripleCDataSetTableAdapters.QueriesTableAdapter
            da.asignarEstudiante_Equipo(pidE, pidEq)
        Else
            Throw New ArgumentException("No puede incluir estudiante al equipo" & vbNewLine & "       ya tiene notas evaluadas!!!")
        End If
    End Sub

    Public Sub escluirEstudianteEquipo(ByVal pidE As Integer)
        Dim da As New accesoBD.TripleCDataSetTableAdapters.QueriesTableAdapter
        da.escluirEstudiante_Equipo(pidE)
    End Sub

    Public Sub registrarEquipo(ByVal pnombre As String)
        Dim objEq As Equipo
        objEq = New Equipo()
        objEq.insertarDatos(Me.idGrupo, pnombre)
        objEq.insertese()
    End Sub

    Public Sub modificarEquipo(ByVal pidE As Integer, ByVal pnombre As String)
        Dim objEq As Equipo
        objEq = New Equipo(pidE)
        objEq.insertarDatos(Me.idGrupo, pnombre)
        objEq.actualicese()
    End Sub

    Public Sub eliminarEquipo(ByVal pidE As Integer)
        Dim dt As New DataTable
        Try
            dt = New accesoBD.TripleCDataSetTableAdapters.selectNotasEquipo_EquipoTableAdapter().GetData(pidE)
            If Not (dt.Rows.Count > 0) Then
                Dim da As New accesoBD.TripleCDataSetTableAdapters.QueriesTableAdapter
                da.deleteEquipo(pidE)
            End If
        Catch ex As Exception
            Throw New ArgumentException("Existe notas evaluadas al equipo")
        End Try
        
    End Sub

    Public Function darstudiante(ByVal idE As Integer) As DataTable
        Dim dt As DataTable = New accesoBD.TripleCDataSetTableAdapters.selectEstudianteTableAdapter().GetData(Me.idGrupo, idE)
        Return dt
    End Function

    Public Function mostrarPromedioEquipo(ByVal pidE As Integer) As ArrayList
        Dim objE As New Equipo(pidE)
        Return objE.obtenerPromedio()
    End Function

    Public Function mostrarDesgloseEquipo(ByVal pidE As Integer) As DataTable
        Dim objE As New Equipo(pidE)
        Return objE.obtenerDesglose()
    End Function

    Public Sub modificarNotaGrupal(ByVal pidE As Integer, ByVal pidR As Integer, ByVal pnota As String)
        Me.obtenerPlan()
        Dim objE As New Equipo(pidE)
        objE.modificarNota(Me.idGrupo, pidR, pnota)
    End Sub

    Public Function obtenerNotasEstudiantes(ByVal pidE As Integer, ByVal pidR As Integer) As DataTable
        Dim dt As New DataTable
        Dim objE As New Equipo(pidE)
        dt = objE.obtenerNotasEstudiantes(pidR)
        Return dt
    End Function

    Public Function obtenerSumatoria(ByVal pidE As Integer, ByVal pidR As Integer) As String
        Dim msj As String
        Dim objE As New Equipo(pidE)
        msj = objE.obtenerSumatoria(pidR)
        Return msj
    End Function

End Class
